<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<fieldset class="form-container " style="width:80%;margin-left:5%;"> 
    <form:form id="form_782213"    method="post" action="/contrat/add.do" modelAttribute="contrat" autocomplete="on">
        <legend class="form-title"><h2>Cr&eacute;ation d'un contrat</h2></legend>
        <h3 style="margin-bottom: 5%;margin-left: 1%">Veuillez renseigner les champs ci-dessous :</h3>
        <form:errors path="*" cssClass="errorblock" element="div"></form:errors>
        <form:hidden path="id"/>
            <div style="float: left;position: relative;">
                <p class="form-title">Rechercher un Adherent
                    <input type="text" id="rechercheAdherent" name="adherentRecherche" value="${adherentRecherche}"/>
                <button class="submit-button" id="rechercheAdherentButton" style="padding: 2.5px 8px;">Recherche</button></p>
            <div id="resultAdhrent"></div>

        </div>
        <div style="float: left;position: relative;margin-left :50px;">
            <p class="form-title">Rechercher un Employ&eacute;
                <input type="text" id="rechercheEmploye" name="employeRecherche" value="${employeRecherche}"/>
                <button class="submit-button" id="rechercheEmployeButton" style="padding: 2.5px 8px;">Recherche</button></p>
            <div id="resultEmploye"></div>

        </div>

        <div style=" clear: left;float: left;position: relative;" >
            <div class="form-title">Date entr&eacute;e </div>
            <form:input class="form-field" type="text" path="dateEntree"/>
            <div class="form-title">Taux de Base </div>
            <form:input class="form-field" type="text" path="salaireBase"/>
        </div>
        <div style=" float: left;position: relative;margin-left: 2%;" >
            <div class="form-title">Mod&egrave;le:</div>
            <form:select  cssClass="form-field" path="modelPaie.id" id="modelPaieSelect">

                <c:forEach items="${listModelPaie}" var="model">
                    <form:option value="${model.id}">${model.nom}</form:option>
                </c:forEach>
            </form:select>
        </div>
        <div class="submit-container" style=" clear: left;">
            <form:button class="submit-button" type="submit" >Enregistrer</form:button>
           <a href="/contrat/index.do" class="submit-button">Annuler</a>
            </div>
    </form:form>
</fieldset>

<script>
    $(function() {
        $('#dateEntree').datepicker({dateFormat: 'dd mm yy'})
        
        $("button[id=rechercheAdherentButton]")
                .click(function(event) {
                    event.preventDefault();
                    event.preventDefault();
                    $.ajax({
                        url: '/contrat/searchAdherent.do?nomPrenom=' + $('#rechercheAdherent').val(),
                        type: "POST",
                        beforeSend: function(xhr) {

                            xhr.setRequestHeader("Accept", "application/json");

                            xhr.setRequestHeader("Content-Type", "application/json");

                        },
                        success: function(adherent) {
                            if (adherent.length == 0) {
                                var string = "Aucun résultat";
                            } else {
                                var string = "<div class=\"datagrid\"><table ><thead><tr><th>Nom</th><th>Sélectionner</th</tr></thead>";
                                for (i = 0; i < adherent.length; i++) {
                                    string += "<tr><td>" + adherent[i].nomPrenom + "</td><td><input type=\"radio\" name=\"adherent.id\" value=" + adherent[i].id + "></td></tr>";
                                }




                                string += "</tbody></table></div>";
                            }
                            $('#resultAdhrent').html(string);


                        }

                    });
                });
        $("button[id=rechercheEmployeButton]")
                .click(function(event) {
                    event.preventDefault();
                    $.ajax({
                        url: '/contrat/searchEmploye.do?nom=' + $('#rechercheEmploye').val(),
                        type: "POST",
                        beforeSend: function(xhr) {

                            xhr.setRequestHeader("Accept", "application/json");

                            xhr.setRequestHeader("Content-Type", "application/json");

                        },
                        success: function(adherent) {
                            if (adherent.length == 0) {
                                var string = "Aucun résultat"
                            } else {
                                var string = "<div class=\"datagrid\"><table ><thead><tr><th>Nom</th><th>Sélectionner</th</tr></thead>";
                                for (i = 0; i < adherent.length; i++) {
                                    string += "<tr><td>" + adherent[i].nom + " " + adherent[i].prenom + "</td><td><input type=\"radio\" name=\"employe.id\" value=" + adherent[i].id + "></td></tr>"
                                }




                                string += "</tbody></table></div>"
                            }
                            $('#resultEmploye').html(string);


                        }

                    });
                });
    });
</script>

<spring:hasBindErrors name="contrat">
    <script>
        $(function() {
        <c:if test="${contrat.adherent!=null}">
            $.ajax({
                url: '/contrat/searchAdherent.do?nomPrenom=${adherentRecherche}',
                type: "POST",
                beforeSend: function(xhr) {

                    xhr.setRequestHeader("Accept", "application/json");

                    xhr.setRequestHeader("Content-Type", "application/json");

                },
                success: function(adherent) {
                    if (adherent.length == 0) {
                        var string = "Aucun résultat";
                    } else {
                        var string = "<div class=\"datagrid\"><table ><thead><tr><th>Nom</th><th>Sélectionner</th</tr></thead>";
                        for (i = 0; i < adherent.length; i++) {
                            string += "<tr><td>" + adherent[i].nomPrenom + "</td><td><input type=\"radio\" name=\"adherent.id\" value=" + adherent[i].id + "></td></tr>";
                        }




                        string += "</tbody></table></div>";
                    }
                    $('#resultAdhrent').html(string);
                    $("input[type='radio'][name='adherent.id'][value=${contrat.adherent.id}]").prop('checked', true);

                }

            });
        </c:if>
        <c:if test="${contrat.employe!=null}">
            $.ajax({
                url: '/contrat/searchEmploye.do?nom=${employeRecherche}',
                type: "POST",
                beforeSend: function(xhr) {

                    xhr.setRequestHeader("Accept", "application/json");

                    xhr.setRequestHeader("Content-Type", "application/json");

                },
                success: function(adherent) {
                    if (adherent.length == 0) {
                        var string = "Aucun résultat"
                    } else {
                        var string = "<div class=\"datagrid\"><table ><thead><tr><th>Nom</th><th>Sélectionner</th</tr></thead>";
                        for (i = 0; i < adherent.length; i++) {
                            string += "<tr><td>" + adherent[i].nom + " " + adherent[i].prenom + "</td><td><input type=\"radio\" name=\"employe.id\" value=" + adherent[i].id + "></td></tr>"
                        }




                        string += "</tbody></table></div>"
                    }
                    $('#resultEmploye').html(string);
                    $("input[type='radio'][name='employe.id'][value=${contrat.employe.id}]").prop('checked', true);

                }

            });
        </c:if>

        });
    </script>
</spring:hasBindErrors>